//字符字典树
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int son[N][30];//存储 Trie 树的 “边”（节点间的连接）
int cnt[N];//标记 “单词结尾节点” 的出现次数
int idx;//分配节点的唯一 ID
void insert_trie(string str)
{
    int p = 0;
    for(int i = 0; i < str.size(); i ++){
        int tmp = str[i] - 'a';
        if(son[p][tmp] == 0) son[p][tmp] = ++idx;
        p = son[p][tmp];
    }
    cnt[p] ++;
}
int count_num_trie(string str)
{
    int p = 0;
    for(int i = 0; i < str.size(); i ++){
        int tmp = str[i] - 'a';
        if(son[p][tmp] == 0) return 0;
        else p = son[p][tmp];
    }
    return cnt[p];
}
int main()
{
    int n;
    char op;
    cin >> n;
    while(n --)
    {
        string s;
        cin >> op >> s;
        if(op == 'I') 
            insert_trie(s);
        else 
            cout << count_num_trie(s) << endl;
    }
}

/*清空代码
for(int i = 0; i <= idx; i ++)
    {
        cnt1[i] = 0;
        cnt[i] = 0;
        for (int j=0; j<75; j++)
            son[i][j]=0;
    }
    idx = 0;
*/